package com.suanfa;


import com.hanhai.JDK8Date;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;
import java.util.SimpleTimeZone;

/**
 * 计算活了多少天
 * @Author weijingchen
 * @Date 2025/5/27 21:13
 * @Version 1.0.0
 */
public class Test02 {
    public static void main(String[] args) throws ParseException {


        int i = Jdk7Test();
        System.out.println(i);
        int j = JDK8Test();
        System.out.println(j);

        System.out.println("-----------");
        Long l = JDK8Duration();
        System.out.println(l);


        System.out.println("-----------");
        Long l1= JDK8Period();
        System.out.println(l1);
    }

    //无法计算，只能分别计算年月日，然后相加，无法精确到天数
    private static Long JDK8Period() {
        LocalDate  now = LocalDate.now();
        LocalDate of = LocalDate.of(1996, 3, 30);
        Period  between = Period.between(of, now);
        int days = between.getDays();
        return (long) days;
    }


    private static Long JDK8Duration() {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime of = LocalDateTime.of(1996, 3, 30,  0, 0, 0);
        Duration between = Duration.between(of, now);
        return between.toDays();
    }

    //chronoUnit
    private static int JDK8Test() {
        LocalDateTime now = LocalDateTime.now();
        System.out.println(now);
        LocalDateTime localDateTime = LocalDateTime.of(1996, 3, 30, 0, 0, 0);
        long between = ChronoUnit.DAYS.between(localDateTime, now);
        return (int) between;
    }

    private static int Jdk7Test() throws ParseException {

        Date date = new Date();
        long time = date.getTime();
        System.out.println(time);

        String birthday = "1996-03-30";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        Date parse = sdf.parse(birthday);

        long time1 = parse.getTime();
        System.out.println(time1);
        long l = time - time1;
        long l1 = l / (1000 * 60 * 60 * 24);
        return (int) l1;
    }
}
